<?php

/**
 * 
 * @author yuxun
 *
 */
class DataCenterService extends Service {
	
	/**
	 * 获取专服列表
	 */
	public static function getPlatList(){
		$modSpecialServer = new ModSpecialServer();
		$params = array();
		$params['g'] = 'mango';
		$platList = $modSpecialServer->select($params);
		$data = array();
		foreach ($platList as $key=>$val){
			$d = array();
			$d['plat_id'] = $val['special_id'];
			$d['plat_cname'] = $val['cname'];
			$d['plat_name'] = $val['name'];
			$d['status'] = $val['status'];
			array_push($data, $d);
		}
		$json_arr = $data;
		return $json_arr;
	}
	
	/**
	 * 获取服务器列表
	 */
	public static function getServerList(){
		$modServer = new ModServer();
		$params = array();
		$params['g'] = 'mango';
		$serverList = $modServer->select($params);
			
		$modSpecialServicer = new ModSpecialServicer();
		$spList = $modSpecialServicer->getSpecialServer();
		$specialList = array();
		foreach ($spList as $key=>$val){
			$specialList[$val['server_id']]['name'] = $val['name'];
			$specialList[$val['server_id']]['cname'] = $val['cname'];
			$specialList[$val['server_id']]['special_flag_id'] = $val['special_flag_id'];
		}
			
		$data = array();
		foreach ($serverList as $key=>$val){
			$d = array();
			$d['plat_id'] = $specialList[$val['id']]['special_flag_id'];
			$d['plat_cname'] = $specialList[$val['id']]['cname'];
			$d['server_id'] = $val['server_num'];
			$d['name'] = $val['server_name'];
			$d['openday'] = $val['open_time'];
			$d['status'] = $val['show_status'];
			$d['hefu_flag'] = $val['hefu_flag'];
			$d['check_flag'] = $val['check_flag'];
			array_push($data, $d);
		}
		
		$json_arr = $data;
		return $json_arr;		
	}
	
	/**
	 * 获取内玩账号列表
	 */
	public static function getInsideList(){
		$modInside = new ModInside();
		$params = array();
		$params['g'] = 'mango';
		$insideList = $modInside->select($params);
			
		$modSpecialServer = new ModSpecialServer();
		$params = array();
		$params['g'] = 'mango';
		$special_array = $modSpecialServer->select($params);
		$specialList = array();
			
		foreach ($special_array as $key=>$val){
			$specialList[$val['id']] = $val['special_id'];
		}
			
		$modServer = new ModServer();
		$params = array();
		$params['g'] = 'mango';
		$server_array = $modServer->select($params);
		$serverList = array();
		foreach ($server_array as $key=>$val){
			$serverList[$val['id']] = $val['server_num'];
		}
			
		$modChannel = new ModChannel();
		$params = array();
		$params['g'] = 'mango';
		$channel_array = $modChannel->select($params);
		$channelList = array();
		foreach ($channel_array as $key=>$val){
			$channelList[$val['id']] = $val['tab'];
		}
			
		$data = array();
		foreach ($insideList as $key=>$val){
			$d = array();
			$d['plat_id'] = $specialList[$val['special_id']];
			$d['server_id'] = $serverList[$val['server_id']];
			$d['ditch'] = $channelList[$val['channel_id']];
			$d['user'] = $val['openid'];
			$d['username'] = $val['username'];
			$d['ctime'] = $val['create_time'];
			array_push($data, $d);
		}

		$json_arr = $data;
		return $json_arr;	
	}
	
	/**
	 * 获取游戏服登陆人数
	 * @param unknown $plat_id
	 * @param unknown $server_id
	 * @param unknown $date
	 * @param unknown $time
	 */
	public static function getLoginNum($plat_id,$server_id,$date,$time){
		if($plat_id == ''){
			$json_arr = array('state'=>1,'msg'=>'need to parameter plat_id');
			return $json_arr;
		}
			
		if($server_id == ''){
			$json_arr = array('state'=>1,'msg'=>'need to parameter server_id');
			return $json_arr;
		}
			
		if($date == ''){
			$json_arr = array('state'=>1,'msg'=>'need to parameter date');
			return $json_arr;
		}
			
		if($time == ''){
			$json_arr = array('state'=>1,'msg'=>'need to parameter time');
			return $json_arr;
		}
		
		$is_date = strtotime($date)? strtotime($date) : false;
		
		if($is_date===false){
			$json_arr = array('state'=>1,'msg'=>'Date format illegal');
			return $json_arr;
		}
		
		//根据$date（日期）$server_id（服务器）从登录日志查询
		$modServer = new ModServer();
		$server_obj = $modServer->findByAttributes(array('server_num' => $server_id));
		if($server_obj){
			$server_info = $server_obj->getAttributes();
		}else{
			$json_arr = array('state'=>1,'msg'=>'server_id is not found');
			return $json_arr;
		}
		
		$server = $server_info['develop_id'];

		
		$list = self::getLoginList($server,$date);
		
		if($list == false && count($list) != 0){
			$json_arr = array('state'=>1,'msg'=>'mysql error');
			return $json_arr;
		}
		
		$num = count($list);
		$json_arr = array('state'=>0,'msg'=>'success','result'=>$num);
		return $json_arr;
		
	}
	
	
	public static function getLoginList($server,$date){
		$logServer = PubServer::getLogServerByDevId($server);
		$port = '3309';  //正式的是3309。测试的是3308
		//echo $logServer;exit;
		
		if(empty($logServer)){
			return false;
		}
		
		$username = Yii::app()->params['ModLog']['username'];
		$password = Yii::app()->params['ModLog']['password'];

		$config = array(
				'dsn'       =>  'mysql:host='.$logServer.';port='.$port,
				'username' =>  $username,
				'password' =>  $password,
		);
		
		$data_str = strtotime($date);
		$year_moth = date('Ym',$data_str);
	
		$dbName = 'mangologdb_'.$year_moth;
		
		
		$year_month_day = date('Ymd',$data_str);

		$table = 'tbllog_login_'.$year_month_day;
		$sql = "select openid FROM ".$table." where server = '".$server."' group by openid";
		
		$dbLink = new PubDbLink($config, $dbName);
		
		if(!$dbLink){
			return false;
		}
		
		$list = $dbLink->query($sql);
		
		return $list;
	}
	
	
}